package com.pa.scratch.controller;


import com.github.pagehelper.PageInfo;
import com.lww.springcloudapi.entity.TabGgcardEncash;
import com.lww.springcloudapi.entity.TabGgcardObtain;
import com.lww.springcloudapi.entity.TabSaleplanGiftlog;
import com.lww.springcloudapi.result.Result;
import com.lww.springcloudapi.result.ResultCodeEnum;
import com.lww.springcloudapi.vo.TabSaleplanGiftlogVo;
import com.pa.scratch.mapper.TabSaleplanGiftlogMapper;
import com.pa.scratch.service.TabSaleplanGiftlogService;
import io.swagger.annotations.Api;
import lombok.val;
import org.apache.poi.hssf.usermodel.*;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 营销礼品异动表 前端控制器
 * </p>
 *
 * @author SHEN
 * @since 2021-03-11
 */
@Api(description = "礼品领取日/月报表")
@RestController
@RequestMapping("/userTest/tab-saleplan-giftlog")
public class TabSaleplanGiftlogController {
    @Autowired
    TabSaleplanGiftlogService tabSaleplanGiftlogService;

    @PostMapping("/scratch/selectGiftDay")
    public Result selectGiftDay(@RequestParam(required = false,defaultValue = "1") int pageNo,
                                @RequestParam(required = false,defaultValue = "2") int pageSize,
                                @RequestParam(required = false, defaultValue = "")String saleplanname,
                                @RequestParam(required = false, defaultValue = "")String svcid,
                                @RequestParam(required = false, defaultValue = "")String gifttype,
                                @RequestParam(required = false, defaultValue = "")String pnumber,
                                @RequestParam(required = false, defaultValue = "")String y,
                                @RequestParam(required = false, defaultValue = "")String m,
                                @RequestParam(required = false, defaultValue = "")String d) {
//        saleplanname="刮刮卡";
//        svcid="1";
//        gifttype="1";
//        pnumber="13874702983";
//        y="2020";
//        m="01";
//        d="01";
        Map map = new HashMap();
        map.put("saleplanname",saleplanname);
        map.put("svcid",svcid);
        map.put("gifttype",gifttype);
        map.put("pnumber",pnumber);
        map.put("y",y);
        map.put("m",m);
        map.put("d",d);
        try {
        List<TabSaleplanGiftlog> tabSaleplanGiftlogs = tabSaleplanGiftlogService.selectGiftDay(map,pageNo,pageSize);
        PageInfo<TabSaleplanGiftlog> pageInfo = new PageInfo<TabSaleplanGiftlog>(tabSaleplanGiftlogs);
//        System.out.println(tabSaleplanGiftlogs.size());
//        List<TabSaleplanGiftlogVo>  tabSaleplanGiftlogVo = new ArrayList<>();
//        for (TabSaleplanGiftlog tabSaleplanGiftlog :tabSaleplanGiftlogs){
//            TabSaleplanGiftlogVo TabSaleplanGiftlogVo2=new TabSaleplanGiftlogVo();
//            TabSaleplanGiftlogVo2.setSaleplanname(tabSaleplanGiftlog.getTabSaleplanpara().getSaleplanname());
//            TabSaleplanGiftlogVo2.setSvcid(tabSaleplanGiftlog.getTabSaleplanpara().getSvcid());
//            TabSaleplanGiftlogVo2.setGifttype(tabSaleplanGiftlog.getTabSaleplanGiftcode().getGifttype());
//            TabSaleplanGiftlogVo2.setPnumber(tabSaleplanGiftlog.getTabHallUsermessage().getPnumber());
//            TabSaleplanGiftlogVo2.setOptrdate(tabSaleplanGiftlog.getOptrdate());
//            tabSaleplanGiftlogVo.add(TabSaleplanGiftlogVo2);
//        }
//        for (TabSaleplanGiftlogVo tabSaleplanGiftlogVo1:
//             tabSaleplanGiftlogVo) {
//            System.out.println(tabSaleplanGiftlogVo1.getSaleplanname()
//                    +"/"+ tabSaleplanGiftlogVo1.getSvcid()
//                    +"/"+tabSaleplanGiftlogVo1.getGifttype()
//                    +"/"+tabSaleplanGiftlogVo1.getPnumber()
//                    +"/"+tabSaleplanGiftlogVo1.getOptrdate());
//        }
        return new Result().ok(pageInfo);
        } catch (Exception e) {
            return new Result().fail(ResultCodeEnum.FAIL);
        }
    }

    @RequestMapping(value = "/scratch/GiftReceivingDayExcelDownloads", method = RequestMethod.GET)
    public Result downloadAllClassmate(HttpServletResponse response,
                                       @RequestParam(defaultValue = "1") int pageNo,
                                       @RequestParam(defaultValue = "2") int pageSize,
                                       @RequestParam(required = false, defaultValue = "")String saleplanname,
                                       @RequestParam(required = false, defaultValue = "")String svcid,
                                       @RequestParam(required = false, defaultValue = "")String gifttype,
                                       @RequestParam(required = false, defaultValue = "")String pnumber,
                                       @RequestParam(required = false, defaultValue = "")String y,
                                       @RequestParam(required = false, defaultValue = "")String m,
                                       @RequestParam(required = false, defaultValue = "")String d) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
//        saleplanname = "刮刮卡";
//        svcid = "1";
//        gifttype = "1";
//        pnumber = "13874702983";
//        y = "2020";
//        m = "01";
//        d = "01";
        System.out.println(saleplanname+"\n=="+svcid+"\n=="+gifttype+"\n=="+pnumber+"\n==");
        Map map = new HashMap();
        map.put("saleplanname",saleplanname);
        map.put("svcid",svcid);
        map.put("gifttype",gifttype);
        map.put("pnumber",pnumber);
        map.put("y",y);
        map.put("m",m);
        map.put("d",d);
        List<TabSaleplanGiftlog> tabSaleplanGiftlogs = tabSaleplanGiftlogService.selectGiftDay(map,pageNo,pageSize);

        for (TabSaleplanGiftlog tabSaleplanGiftlogs1 :
                tabSaleplanGiftlogs) {
            System.out.println(tabSaleplanGiftlogs1.toString());
        }
        String fileName = "礼品领取日/月报表" + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据

        int rowNum = 1;

        String[] headers = {"营销活动种类", "业务类型", "奖品种类", "用户手机号", "领取日期"};
        //headers表示excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (TabSaleplanGiftlog tabSaleplanGiftlogs1 :
                tabSaleplanGiftlogs) {
            HSSFRow row1 = sheet.createRow(rowNum);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String GiftlogDate = format.format(tabSaleplanGiftlogs1.getOptrdate());//这个就是把时间戳经过处理得到期望格式的时间
            row1.createCell(0).setCellValue(tabSaleplanGiftlogs1.getTabSaleplanpara().getSaleplanname());
            row1.createCell(1).setCellValue(tabSaleplanGiftlogs1.getTabSaleplanpara().getSvcid());
            row1.createCell(2).setCellValue(tabSaleplanGiftlogs1.getTabSaleplanGiftcode().getGifttype());
            row1.createCell(3).setCellValue(tabSaleplanGiftlogs1.getTabHallUsermessage().getPnumber());
            row1.createCell(4).setCellValue(GiftlogDate);
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());

        return new Result().ok();
    }
}

